from __future__ import annotations
from typing import Optional
from sqlalchemy.orm import Mapped, mapped_column, relationship
from extensions import db
from datetime import datetime
from sqlalchemy import func

class Store(db.Model):
    """店铺模型"""
    __tablename__ = 'stores'

    # 自增主键ID，系统内部使用的唯一标识
    id: Mapped[int] = mapped_column(db.Integer, primary_key=True)
    # 店铺唯一标识符，使用UUID格式作为店铺的唯一标识，用于系统间数据关联
    store_id: Mapped[str] = mapped_column(db.String(36), unique=True, index=True)
    # 店铺名称，店铺的营业名称，用于展示和识别
    name: Mapped[str] = mapped_column(db.String(100), nullable=False)
    # 店铺地址，店铺的详细物理地址，用于定位和导航
    address: Mapped[Optional[str]] = mapped_column(db.String(200))
    # 联系电话，店铺的客服电话，用于客户咨询和预约
    phone: Mapped[str] = mapped_column(db.String(20), nullable=False)
    # 店铺描述信息，店铺的详细介绍，包括特色服务和营业信息等
    description: Mapped[Optional[str]] = mapped_column(db.Text)
    # 店铺图片URL，店铺的展示图片链接，用于在APP或网站中展示
    image_url: Mapped[Optional[str]] = mapped_column(db.String(500))
    # 店铺类型，标识店铺的经营类型，如：理发店、美容店等
    store_type: Mapped[Optional[str]] = mapped_column(db.String(50))
    # 店铺营业开始时间
    open_time: Mapped[Optional[str]] = mapped_column(db.String(5))
    # 店铺营业结束时间
    close_time: Mapped[Optional[str]] = mapped_column(db.String(5))
    # 理发师提成比例，理发师的服务提成比例，如：20.00表示提成20%
    hairdresser_commission_ratio: Mapped[Optional[float]] = mapped_column(db.Numeric(5, 2), default=0.0)
    # 洗头师提成比例，洗头师的服务提成比例，如：10.00表示提成10%
    washer_commission_ratio: Mapped[Optional[float]] = mapped_column(db.Numeric(5, 2), default=0.0)
    # 师徒分成比例，师傅从徒弟提成中获得的分成比例，如：30.00表示获得30%
    mentorship_split_ratio: Mapped[Optional[float]] = mapped_column(db.Numeric(5, 2), default=0.0)
    # 记录创建时间，店铺信息首次创建的时间戳
    created_at: Mapped[datetime] = mapped_column(
        db.DateTime, 
        server_default=func.now(),  # 使用数据库当前时间
        default=datetime.now        # 使用Python系统时间作为后备
    )
    updated_at: Mapped[datetime] = mapped_column(
        db.DateTime, 
        server_default=func.now(),  # 使用数据库当前时间
        default=datetime.now,       # 使用Python系统时间作为后备
        onupdate=datetime.now       # 更新时使用Python系统时间
    )
    # 软删除标记，默认为未删除
    is_deleted: Mapped[bool] = mapped_column(db.Boolean, default=False)
    # 删除时间，记录软删除操作的时间
    deleted_at: Mapped[Optional[datetime]] = mapped_column(db.DateTime)
    # 加密后的登录密码
    password_hash: Mapped[Optional[str]] = mapped_column(db.String(128))

    # 店长ID，每个店铺必须有一个店长
    manager_id: Mapped[str] = mapped_column(db.String(36))
    
    def __init__(self, store_id, name, address, phone, manager_id, description=None, image_url=None, store_type=None,
                 hairdresser_commission_ratio=0.0, washer_commission_ratio=0.0, mentorship_split_ratio=0.0,
                 open_time=None, close_time=None):
        self.store_id = store_id
        self.name = name
        self.address = address
        self.phone = phone
        self.manager_id = manager_id
        self.description = description
        self.image_url = image_url
        self.store_type = store_type
        self.hairdresser_commission_ratio = hairdresser_commission_ratio
        self.washer_commission_ratio = washer_commission_ratio
        self.mentorship_split_ratio = mentorship_split_ratio
        self.open_time = open_time
        self.close_time = close_time

    def set_password(self, password):
        from werkzeug.security import generate_password_hash
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        from werkzeug.security import check_password_hash
        if self.password_hash is None:
            return False
        return check_password_hash(self.password_hash, password)

    def to_dict(self):
        """将模型转换为字典"""
        return {
            'id': self.id,
            'store_id': self.store_id,
            'name': self.name,
            'address': self.address,
            'phone': self.phone,
            'description': self.description,
            'image_url': self.image_url,
            'store_type': self.store_type,
            'hairdresser_commission_ratio': float(self.hairdresser_commission_ratio) if self.hairdresser_commission_ratio is not None else None,
            'washer_commission_ratio': float(self.washer_commission_ratio) if self.washer_commission_ratio is not None else None,
            'mentorship_split_ratio': float(self.mentorship_split_ratio) if self.mentorship_split_ratio is not None else None,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'manager_id': self.manager_id,
            'open_time': self.open_time,
            'close_time': self.close_time
        }